package org.javaboy.remember_me_demo.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.Customizer;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.web.SecurityFilterChain;

@Configuration
public class SecurityConfig {

    @Bean
    SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
        http.authorizeHttpRequests(a -> a
                        .requestMatchers("/hello").authenticated()
                        .requestMatchers("/rm").rememberMe()
                        //必须是用户名密码登录才能访问这个接口
                        .requestMatchers("/fullAuth").fullyAuthenticated()
                        .anyRequest().authenticated())
                .formLogin(f->f.loginPage("/login.html").loginProcessingUrl("/login").permitAll())
                .rememberMe(r -> r
                        //这个地方可以配置 RememberMe 令牌的有效期
//                        .tokenValiditySeconds()
                        //这个参数设置为 true，就不用在登录页面勾选 RememberMe 了
//                        .alwaysRemember(true)
                        .key("javaboy"))
                .csrf(c -> c.disable());
        return http.build();
    }
}
